<?php
/**
* @property CI_Loader $load
* @property CI_Form_validation $form_validation
* @property CI_Input $input
* @property CI_Email $email
* @property CI_DB_active_record $db
*
*/
class Complain extends CI_Model
{
    function get_category()
    {
        $this->db->select('category');
        $this->db->distinct();
        $query = $this->db->get('fault');
        return  $query->result();
    }
    function get_faultlist($category)
    {
        $this->db->select('faultid,description,title');
        $this->db->where('category',$category);
        $query = $this->db->get('fault');
        return $query->result_array();
    }

    function insert($phoneno,$faultid,$since)
    {


        $this->db->set('status','Recent');
        $this->db->set('message','Your application is sent to Divisional Engineer');
        $this->db->set('faultid',$faultid);
        $this->db->set('since',$since);
        $this->db->set('phoneno',$phoneno);
        $this->db->insert('complain');

        return $this->db->insert_id();
    }
    function insert_handle($code,$empid,$appid)
    {
        
        $this->db->set('code',$code);
        $this->db->set('empid',$empid);
        $this->db->set('appid',$appid);
        $this->db->insert('complainhandle');
    }
    function submit_complain($code,$empid,$phoneno,$faultid,$other,$since)
    {
        $this->db->trans_start();
        $appid = $this->insert($phoneno, $faultid, $since);
        $handle = $this->insert_handle($code, $empid, $appid);
        if($other!=NULL)
        {
        $this->db->set('description',$other);
        $this->db->set('faultid',$faultid);
        $this->db->set('appid',$appid);
        $this->db->insert('other');
        }
        $this->db->trans_complete();

        return $appid;
    }
    function already_complain($phoneno,$faultid)
    {
         $this->db->where('phoneno',$phoneno);
        $this->db->where('faultid',$faultid);
        $this->db->where('accomplishdate is NULL');
        $query = $this->db->get('complain');
        if($query->num_rows()==0)
         return false;
        return true;
    }
    function get_complaints($id,$status,$category)
    {
        
        $query = $this->db->query("SELECT
                                    complain.appid,
                                    complain.applydate,
                                    fault.title,
                                    complain.accomplishdate
                                    FROM
                                    complain
                                    Inner Join fault ON complain.faultid = fault.faultid
                                    Inner Join complainhandle ON complain.appid = complainhandle.appid
                                    WHERE
                                    complainhandle.empid = '$id' AND
                                    fault.category =  '$category' AND
                                    complain.status =  '$status'");
        return $query->result_array();

    } 
    function get_complain_detail($appid)
    {
            $query = $this->db->query("SELECT
                                    complain.appid,
                                    complain.since,
                                    complain.phoneno,
                                    other.description,
                                    customer.FirstName,
                                    customer.LastName
                                    FROM
                                    complain
                                    Left Outer Join other ON complain.faultid = other.faultid AND complain.appid = other.appid ,
                                    customer
                                    Inner Join telephone ON phoneno = telephone.phoneno AND telephone.Id = customer.Id
                                    where complain.appid = $appid");
            return $query->row();
    }

    function get_action($appid){
        $query = $this->db->query("SELECT
                                    action.id,
                                    action.startdate,
                                    action.enddate,
                                    action.description
                                    FROM
                                    action                                    
                                    WHERE
                                    action.appid =  '$appid'");
        return $query->result_array();
    }
   function get_actionbyid($id){
        $query = $this->db->query("SELECT
                                    action.id,
                                    action.startdate,
                                    action.enddate,
                                    action.description
                                    FROM
                                    action
                                    WHERE
                                    action.id =  '$id'");
        return $query->row_array();
    }
    
    function add_action($appid,$desc){
        $this->db->set('description',$desc);
        $this->db->set('appid',$appid);
        $this->db->insert('action');
        $id=$this->db->insert_id();
        return $this->get_actionbyid($id);

    }
    function update_status($id,$status)
    {

        $this->db->set('status', $status);
        $this->db->where("appid",$id);
        $this->db->update('complain');

    }
    function update_message($appid,$msg)
    {

        $this->db->set('message', $msg);
        $this->db->where("appid",$appid);
        $this->db->update('complain');
    }
    function get_appid($phone)
    {
        $query = $this->db->query("SELECT
                                    complain.appid
                                    FROM
                                    complain
                                    WHERE phoneno = '$phone'
                                    ORDER BY
                                    complain.applydate DESC");
        return $query->result();
    }
    function get_status($input)
    {
        $this->db->where($input);
        $query = $this->db->get('complain');
        if($query->num_rows() > 0)
         {
            $this->db->where('appid',$input['appid']);
            $query = $this->db->get('complain');
            return $query->row_array();
         }
         else
         {
             $data['appid']=-1;
             return $data;
         }

    }
}

?>
